package de.rj.urwalking.ui.fragments.dialog;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

import com.actionbarsherlock.app.SherlockDialogFragment;

import de.rj.urwalking.R;
import de.rj.urwalking.data.Experiment;
import de.rj.urwalking.db.DBExperiment;
import de.rj.urwalking.ui.activities.HybridActivity;
import de.rj.urwalking.ui.activities.IndoorActivity;
import de.rj.urwalking.ui.activities.OutdoorActivity;
import de.rj.urwalking.ui.activities.URwalkingActivity;
import de.rj.urwalking.util.ExperimentManager;
import de.rj.urwalking.util.ExperimentsAdapter;
import de.rj.urwalking.util.PrefUtil;

/**
 * Shows a list of available experiments.
 * 
 * @author rjj
 * 
 */
public class ExperimentsDialogFragment extends SherlockDialogFragment {

	public static final String EXPERIMENT_TYPE = "experimentType";
	public static final String ACTIVITY_TYPE = "activityType";

	public ExperimentsDialogFragment() {
		// Empty constructor required for DialogFragment
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_experiments_dialog, container);
		getDialog().setTitle(getString(R.string.choose_experiment));

		setupButton(view);

		ArrayList<DBExperiment> availableExperiments = ExperimentManager.getInstance()
				.getAvailableExperiments(isExperimentOutdoor());

		setupExperimentList(view, availableExperiments);

		return view;

	}

	private void setupExperimentList(View view, ArrayList<DBExperiment> availableExperiments) {
		ListView experimentListView = (ListView) view.findViewById(R.id.experimentsList);

		final ExperimentsAdapter adapter = new ExperimentsAdapter(getActivity(),
				availableExperiments);
		experimentListView.setAdapter(adapter);

		experimentListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

				final DBExperiment item = (DBExperiment) parent.getItemAtPosition(position);
				ExperimentManager.getInstance().chooseExperiment(item);

				dismiss();

				if (item.isBlnoutdoor()) {
					Intent intent = null;
					if (getArguments().getInt(ACTIVITY_TYPE) == URwalkingActivity.TYPE_OUTDOOR) {
						intent = new Intent(getActivity(), OutdoorActivity.class);
					} else if (getArguments().getInt(ACTIVITY_TYPE) == URwalkingActivity.TYPE_HYBRID) {
						intent = new Intent(getActivity(), HybridActivity.class);
					}
					PrefUtil.saveBoolean("allowViewChange", true, getActivity());
					PrefUtil.saveInt("experimentType", Experiment.TYPE_OUTDOOR, getActivity());
					getActivity().startActivity(intent);
				} else {
					Intent intent = new Intent(getActivity(), IndoorActivity.class);
					PrefUtil.saveBoolean("allowViewChange", false, getActivity());
					PrefUtil.saveInt("experimentType", Experiment.TYPE_INDOOR, getActivity());
					getActivity().startActivity(intent);
				}
			}
		});
	}

	/**
	 * 
	 * @return true if outdoor, false if indoor
	 */
	private boolean isExperimentOutdoor() {
		if (getArguments() != null) {
			int type = getArguments().getInt(EXPERIMENT_TYPE);
			if (type == Experiment.TYPE_OUTDOOR) {
				return true;
			} else if (type == Experiment.TYPE_INDOOR) {
				return false;
			}
		}
		return true;
	}

	private void setupButton(View view) {
		Button cancelButton = (Button) view.findViewById(R.id.cancelButton);
		cancelButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dismiss();
			}
		});
	}

}
